﻿#pragma once

#include "common.h"
#include "ui/ui.h"
#include "ui/button.h"

namespace anip
{
	namespace ui
	{
		class Menu;

		class MenuBar : public Control
		{
		protected:
			bool _active = false;

		public:
			class Item : public WithText<Control>
			{
			private:
				Menu* _menu = nullptr;
				bool _doNotSpawnThisTime = false;
				void _spawnMenu();

			protected:
				void processMouseEnter() override;
				void processMouseLeave() override;
				void processMouseButton(MouseButton btn, ButtonAction action) override;

			public:
				Item(MenuBar& m, const wstring& text);

				std::function<Menu*(Item& self)> getSubMenu;
				std::function<void(Item& self)> onClick;

				bool enabled = true;
				bcolor hoverColor = r::MenuItem_HoverColor;
				bfloat pressFactor = r::Button_PressFactor;

				void paint() override;
				void flow(Flow& f) override;
			};

			MenuBar(Container* parent);

			bcolor backColor = r::Menubar_BackColor;

			void clearAllItems();
			Item* firstItem() { return (Item*)_firstChild; }
			Item* lastItem() { return (Item*)_lastChild; }

			void paint() override;
			void flow(Flow& f) override;
			//void applyTheme(Theme& t) override;
		};

		class Menu : public Control
		{
		protected:
			void processDefocus() override;
			Menu* _subMenu = nullptr;

		public:
			class Item : public Control
			{
			protected:
				Menu& _menu;
				void _reportSubMenu(Menu* val);
				Menu* _getParentSubMenu();

			public:
				Item(Menu& m);

				Menu& menu() { return _menu; }
			};

			friend class Menu::Item;

			Menu(Container* parent, vec2 pos = { 0, 0 });

			bool isDropdown = false;
			bfloat roundRadius = r::Menu_RoundRadius;
			bcolor backColor = r::Menu_BackColor;

			//void removeItem(Item& item);
			void clearAllItems();
			Item* firstItem() { return (Item*)_firstChild; }
			Item* lastItem() { return (Item*)_lastChild; }

			void paint() override;
			void flow(Flow& f) override;
		};
	}
}